package vn.edu.ptithcm.pim.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import vn.edu.ptithcm.pim.constant.Constants;
import vn.edu.ptithcm.pim.constant.UrlConstants;
import vn.edu.ptithcm.pim.dto.ChangePasswordDto;
import vn.edu.ptithcm.pim.service.UserService;
import vn.edu.ptithcm.pim.util.ParamUtil;

@SuppressWarnings("deprecation")
public class ChangePasswordController extends SimpleFormController {
	private UserService userService;
	Logger log = Logger.getLogger(ChangePasswordController.class);

	public ChangePasswordController() {
		super();
		setSessionForm(true);
		setCommandClass(ChangePasswordDto.class);
		setCommandName("changePassword");
		setFormView(UrlConstants.CHANGE_PASSWORD_URL);
		setSuccessView(UrlConstants.CHANGE_PASSWORD_URL);
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		ChangePasswordDto changePasswordDto = null;
		if (ParamUtil.getObject(request, "changePasswordDto", null) != null) {
			changePasswordDto = (ChangePasswordDto) ParamUtil.getObject(
					request, "changePasswordDto", null);
		}
		if (changePasswordDto == null) {
			changePasswordDto = new ChangePasswordDto();
		}
		return changePasswordDto;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ChangePasswordDto changePasswordDto = (ChangePasswordDto) command;
		String loginName = userService.getCurrentLogin(request);
		boolean changePasswordOK = userService.changePassword(loginName,
				changePasswordDto.getCurrentPassword(),
				changePasswordDto.getNewPassword1(), null);
		request.setAttribute("changePasswordOK", changePasswordOK);
		request.setAttribute("changePasswordDto", changePasswordDto);
		return showForm(request, response, errors);
	}

	@Override
	protected ModelAndView showForm(HttpServletRequest request,
			HttpServletResponse response, BindException errors)
			throws Exception {
		if (userService.isSignedIn(request)) {
			Map<String, Object> bean = new HashMap<String, Object>();
			String loginName = userService.getCurrentLogin(request);
			bean.put("isSigned", true);
			bean.put(Constants.LOGIN_NAME, loginName);
			bean.put(Constants.USER_NAME, userService.getUsername(loginName));
			String returnToFullPageURL = ParamUtil.getString(request,
					Constants.RETURN_TO_FULL_PAGE_URL, "");
			if (returnToFullPageURL.isEmpty()) {
				returnToFullPageURL = request.getHeader("referer");
			}
			bean.put(Constants.RETURN_TO_FULL_PAGE_URL, returnToFullPageURL);
			request.setAttribute("bean", bean);
		} else {
			String redirectLink = UrlConstants.CHANGE_PASSWORD_URL
					+ UrlConstants.HTM;
			request.getSession().setAttribute("redirectLink", redirectLink);

			response.sendRedirect(UrlConstants.HOME_PAGE_URL + UrlConstants.HTM);
		}
		return super.showForm(request, response, errors);
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
